package com.fzu.mmdbproxyframework.multitask;


import java.sql.SQLException;

import org.springframework.stereotype.Service;

import com.fzu.mmdbproxyframework.context.ClusterContext;
import com.fzu.mmdbproxyframework.context.ConfigContext;
import com.fzu.mmdbproxyframework.model.DBInterf;
import com.fzu.mmdbproxyframework.model.DataSource;
import com.fzu.mmdbproxyframework.model.MMDataNodeCluster;
import com.fzu.mmdbproxyframework.model.MysqlDB;


public class MySQLHeartbeatTask extends ScheduledTask {
	public MMDataNodeCluster cluster;
	public MySQLHeartbeatTask(MMDataNodeCluster cluster) {
		this.cluster=cluster;
	}

	@Override
	public void doTask() {
		System.out.println("MySQLHeartbeatTask");
		for(DBInterf db:cluster.getClusterContext().getSourceDBNodeList()){
//			DBInterf db = ClusterContext.getInstance().getSourceDBNodeList().getFirst();
			MysqlDB mysqlDB = (MysqlDB)db;
			DataSource masterdataSource = mysqlDB.getMasterDataSource();
			int MasterRun =0;
			try {
				MasterRun = masterdataSource.getConnection().createStatement().executeUpdate(cluster.getConfigContext().getUpdateStr());
				if(MasterRun == 0){
					System.out.println("  isMasterDown="+MasterRun+"  master"+masterdataSource.getDbName()+" 挂掉啦啊啊啊啊啊");
					masterdataSource.setShutdown(true);
				}
				System.out.println("  MasterRun="+MasterRun+"  "+masterdataSource.getDbName()+"  还活着======");
			} catch (SQLException e) {
				System.out.println("  MasterRun="+MasterRun+"  "+masterdataSource.getDbName()+" 挂掉啦啊啊啊啊啊");
				masterdataSource.setShutdown(true);
				e.printStackTrace();
			}
			System.out.println("MySQLHeartbeatTask");
		}
	}
	@Override
	public long getDelay() {
		return cluster.getConfigContext().getKeepaliveTime();
	}
	@Override
	public void setStop(boolean stop) {
		super.stop = stop;
	}
	

}

